import os
import pandas as pd
import numpy as np
import csv

df_val = pd.read_csv("csv_results/all.csv")
df_targ = pd.read_csv("csv_results/standard.csv")

goal_ids = [int(eval(v)) for v in df_val.columns.values.tolist()[1:]]

val_values = df_val.values[:, 1:]
targ_values = df_targ.values[:, 1]
print(val_values.shape)
print(targ_values.shape)

prog = 10 - np.abs(targ_values - val_values) / np.abs(targ_values - val_values[0]) * 10

num = np.zeros(17)
table1 = np.zeros((prog.shape[0], 17))

for i in range(prog.shape[1]):
    num[goal_ids[i] - 1] += 1
    table1[:, goal_ids[i] - 1] += prog[:, i]
table1 /= num
table = np.concatenate((df_val.values[:, 0][:, None], table1), axis=-1)

with open("csv_results/prog.csv", "w", newline='', encoding='utf-8') as f:
    writer = csv.writer(f)
    table = table.tolist()
    for line in table:
        line[0] = int(line[0])
    writer.writerow(["year"] + [f"SDG{i}" for i in range(1, 18)])
    writer.writerows(table)
